"""
Case Type   : Timescaledb功能测试
Case Name   : 系统函数_timescaledb_internal.get_partition_for_key测试
Create At   : 2025/05/15
Owner       : lihongji
Description :
              1.调用系统函数get_partition_for_key
Expect      :
              1.调用成功
History     :
"""

import unittest
import os

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class Timescaledb(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.Constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbPGmode')
        self.db_user_node = Node(node='PrimaryDbPGmode')
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status or "Degraded" in status)
        select_extension_cmd = f"SELECT * FROM pg_extension;"
        select_extension_result = self.primary_sh.execut_db_sql(
            select_extension_cmd)
        if "timescaledb" not in select_extension_result:
            create_extension_cmd = f"create extension timescaledb;"
            create_extension_result = (
                self.primary_sh.execut_db_sql(create_extension_cmd))
            self.assertIn(self.constant.extension_create_success,
                          create_extension_result, '创建timescaledb扩展失败')

    def test_add_dimension(self):
        text = '----step1:调用系统函数 expect:调用成功----'
        self.log.info(text)
        get_partition = """
        SELECT _timescaledb_internal.get_partition_for_key('dev1'::text);
        SELECT _timescaledb_internal.get_partition_for_key('dev7'::varchar(5));
        """
        get_partition_result = self.primary_sh.execut_db_sql(get_partition)
        self.log.info(get_partition)
        self.log.info(get_partition_result)
        self.assertIn('449729092', get_partition_result, '执行失败' + text)
        self.assertIn('1129986420', get_partition_result, '执行失败' + text)

    def tearDown(self):
        text = '-----step2:系统函数调用无需清理 expect:成功-----'
        self.log.info(text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
